<?php
//生成自定义菜单
$appid = "wxc6b435d370c78efa";
$appsecret = "64a4288bd91d7758a9e05f4a4835a961";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
//get请求生成accesstoken
//echo $url;
$access_token = gettoken($url);
echo "您的access_token是：".$access_token.'<hr>';
$jsonmenu = '{
    "button": [
        {
            "type": "view", 
            "name": "通知公告", 
            "url": "http://wx.joygov.com/sheyang/notice.html"
        },       
        {
            "name": "在线诉求", 
            "sub_button": [
				{
                    "type": "view", 
                    "name": "我要投诉", 
                    "url": "http://wx.joygov.com/sheyang/wechat/wx.php?action=tousu"
                },
				{
                    "type": "view", 
                    "name": "诉求记录", 
                    "url": "http://wx.joygov.com/sheyang/wechat/wx.php?action=jilu"
                },
				{
                    "type": "click", 
                    "name": "智能咨询", 
                    "key": "欢迎来到智能咨询，请输入您想了解的问题……"
                }
            ]
        },
		{
            "type": "view", 
            "name": "关于我们", 
            "url": "http://wx.joygov.com/sheyang/about.html"
        }, 
    ]
}';


$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $access_token;
//post请求生成菜单

$result = https_request($url, $jsonmenu);

function https_request($url, $data = null)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    $data = curl_exec($ch);
    curl_close($ch);
    print_r($data);

}

function gettoken($url)
{
	$curl = curl_init(); // 启动一个CURL会话
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在
    $data = curl_exec($curl);     //返回api的json对象
    //关闭URL请求
    curl_close($curl);   
    //print_r($data);
    //显示获得的数据
    $response = json_decode($data);	
    return $response->access_token;
}

	
	
